package project.config;



import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http
//                .csrf().disable()
//                .authorizeRequests()
//                .antMatchers("/api/auth/login").permitAll() // 允许登录接口公开访问
//                .anyRequest().authenticated() // 其他接口需要认证
//                .and()
//                .addFilter(new JwtAuthenticationFilter(authenticationManager(), jwtUtils()))
//                .addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUtils()));
//    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .anyRequest().permitAll(); // 其他接口需要认证
    }
//    @Bean
//    public JwtUtils jwtUtils() {
//        return new JwtUtils();
//    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

//    @NoArgsConstructor
//    public class PasswordEncoder extends BCryptPasswordEncoder {
//        @Override
//        public boolean matches(CharSequence rawPassword, String encodedPassword) {
//            // 接收到的前端的密码
//            String pwd = rawPassword.toString();
//            // 进行rsa解密
//            try {
//                pwd = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, pwd);
//            } catch (Exception e) {
//                throw new BadCredentialsException(e.getMessage());
//            }
//            if (encodedPassword != null && encodedPassword.length() != 0) {
//                return BCrypt.checkpw(pwd, encodedPassword);
//            } else {
//                return false;
//            }
//        }
//    }
}